/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormShowStok.java
 *
 * Created on May 5, 2011, 11:52:48 PM
 */

package com.kifas.ui.stok;

import com.kifas.model.xml.BarangModel;
import com.kifas.model.xml.GudangModel;
import com.kifas.model.xml.PengirimanModel;
import com.kifas.model.xml.PenjualanDetailModel;
import com.kifas.model.xml.PenjualanModel;
import com.kifas.model.xml.StokOpnameModel;
import com.kifas.service.MainService;
import com.kifas.service.MasterService;
import com.kifas.service.StokOpnameService;
import java.awt.Component;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.swingbinding.JComboBoxBinding;
import org.jdesktop.swingbinding.SwingBindings;

/**
 *
 * @author dhamarsu
 */
public class FormShowStok extends javax.swing.JInternalFrame {
    private MasterService masterService = new MasterService();
    private StokOpnameService stokOpnameService = new StokOpnameService();
    private List<GudangModel> gudangList;
    private List<StokOpnameModel>  listStok, listStokPo;
    private List<BarangModel> barangs;
    private List<PenjualanModel> penjualanModels;

    /** Creates new form FormShowStok */
    public FormShowStok() {
        initComponents();

        gudangList = masterService.getGudangs();

        barangs =  masterService.getBarangs();

        BindingGroup bindingGroup = new BindingGroup();
        JComboBoxBinding gudangBinding =
                SwingBindings.createJComboBoxBinding(
                UpdateStrategy.READ,
                gudangList,
                cboGudang);

        cboGudang.setRenderer(new DefaultListCellRenderer() {
           @Override
           public Component getListCellRendererComponent(
                   JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
               super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
               if (value instanceof GudangModel) {
                   GudangModel mec = (GudangModel)value;
                   setText(mec.getNamaGudang());
               }
               return this;
           }
        });
        bindingGroup.addBinding(gudangBinding);

        JComboBoxBinding barangBinding =
                SwingBindings.createJComboBoxBinding(
                UpdateStrategy.READ,
                barangs,
                cboBarang);

        cboBarang.setRenderer(new DefaultListCellRenderer() {
           @Override
           public Component getListCellRendererComponent(
                   JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
               super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
               if (value instanceof BarangModel) {
                   BarangModel mec = (BarangModel)value;
                   setText(mec.getNamaBarang());
               }
               return this;
           }
        });
        bindingGroup.addBinding(barangBinding);

        bindingGroup.bind();

        listStok = new ArrayList<StokOpnameModel>();

        refreshTable();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cboGudang = new javax.swing.JComboBox();
        btnRefresh = new javax.swing.JButton();
        chkGudang = new javax.swing.JCheckBox();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel5 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        cboBarang = new javax.swing.JComboBox();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jCheckBox3 = new javax.swing.JCheckBox();
        jCheckBox4 = new javax.swing.JCheckBox();
        btnBySalesOrder = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jTabbedPane3 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblStok = new javax.swing.JTable();
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Form Stok");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Location"));
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel1.setText("Gudang");
        jLabel1.setName("jLabel1"); // NOI18N

        cboGudang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cboGudang.setName("cboGudang"); // NOI18N
        cboGudang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboGudangActionPerformed(evt);
            }
        });

        btnRefresh.setText("Refresh");
        btnRefresh.setName("btnRefresh"); // NOI18N
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        chkGudang.setSelected(true);
        chkGudang.setText("All Gudang");
        chkGudang.setName("chkGudang"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(15, 15, 15)
                .addComponent(cboGudang, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkGudang)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 348, Short.MAX_VALUE)
                .addComponent(btnRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(cboGudang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(btnRefresh)
                .addComponent(jLabel1)
                .addComponent(chkGudang))
        );

        jTabbedPane2.setName("jTabbedPane2"); // NOI18N

        jPanel5.setName("jPanel5"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        jLabel3.setText("Barang");
        jLabel3.setName("jLabel3"); // NOI18N

        cboBarang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cboBarang.setName("cboBarang"); // NOI18N
        cboBarang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboBarangActionPerformed(evt);
            }
        });

        jCheckBox1.setText("Low");
        jCheckBox1.setName("jCheckBox1"); // NOI18N

        jCheckBox2.setText("= 0");
        jCheckBox2.setName("jCheckBox2"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel3)
                .addGap(20, 20, 20)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jCheckBox1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCheckBox2))
                    .addComponent(cboBarang, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(531, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cboBarang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox1)
                    .addComponent(jCheckBox2))
                .addContainerGap(43, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Standard", jPanel2);

        jPanel3.setName("jPanel3"); // NOI18N

        jLabel4.setText("Nama");
        jLabel4.setName("jLabel4"); // NOI18N

        jTextField1.setName("jTextField1"); // NOI18N

        jLabel5.setText("Kode Barang");
        jLabel5.setName("jLabel5"); // NOI18N

        jTextField2.setName("jTextField2"); // NOI18N

        jButton2.setText("Clear");
        jButton2.setName("jButton2"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 442, Short.MAX_VALUE)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(21, 21, 21))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(9, 9, 9)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(38, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Extended", jPanel3);

        jPanel4.setName("jPanel4"); // NOI18N

        jCheckBox3.setText("On PO");
        jCheckBox3.setName("jCheckBox3"); // NOI18N

        jCheckBox4.setText("On DO");
        jCheckBox4.setName("jCheckBox4"); // NOI18N

        btnBySalesOrder.setText("Get Data");
        btnBySalesOrder.setName("btnBySalesOrder"); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCheckBox3)
                            .addComponent(jCheckBox4)))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(btnBySalesOrder, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(720, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jCheckBox3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jCheckBox4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnBySalesOrder)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Sales Order", jPanel4);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 852, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );

        jTabbedPane2.addTab("Filter", jPanel5);

        jPanel6.setName("jPanel6"); // NOI18N

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 852, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 149, Short.MAX_VALUE)
        );

        jTabbedPane2.addTab("Opname", jPanel6);

        jTabbedPane3.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);
        jTabbedPane3.setName("jTabbedPane3"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tblStok.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Gudang", "Nama", "Kadaluarsa", "Tanggal", " Qty Fisik", "Qty PO", "Free Stok"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Double.class, java.lang.Object.class, java.lang.Object.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tblStok.setName("tblStok"); // NOI18N
        jScrollPane1.setViewportView(tblStok);

        jTabbedPane3.addTab("Data Stok", jScrollPane1);

        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        jButton1.setText("View Report");
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setName("jButton1"); // NOI18N
        jButton1.setPreferredSize(new java.awt.Dimension(100, 50));
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton1);

        jButton3.setText("Export Xls");
        jButton3.setFocusable(false);
        jButton3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton3.setName("jButton3"); // NOI18N
        jButton3.setPreferredSize(new java.awt.Dimension(100, 50));
        jButton3.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton3);

        jButton4.setText("Print");
        jButton4.setFocusable(false);
        jButton4.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton4.setName("jButton4"); // NOI18N
        jButton4.setPreferredSize(new java.awt.Dimension(100, 50));
        jButton4.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton4);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTabbedPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 860, Short.MAX_VALUE)
                    .addComponent(jTabbedPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 860, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 860, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addComponent(jTabbedPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cboGudangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboGudangActionPerformed
        // TODO add your handling code here:        

    }//GEN-LAST:event_cboGudangActionPerformed

    private List<StokOpnameModel> calculateStokView(List<StokOpnameModel> listStart){
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        penjualanModels = MainService.getPenjualanService().getPenjualansIsClosed(false);

        listStokPo = new ArrayList<StokOpnameModel>();
        for (PenjualanModel p : penjualanModels){
            List<PenjualanDetailModel> listDetail = new ArrayList<PenjualanDetailModel>(p.getPenjualanDetails());
            for(PenjualanDetailModel pdm : listDetail){

                if (listStokPo.isEmpty()){
                    listStokPo.add(new StokOpnameModel(null, pdm.getBarang(), pdm.getGudang(),
                            pdm.getBulanKadaluarsa(), pdm.getQty().intValue(), 0, 0, 0,  new Date()));
                    continue;
                }
                for (int x = 0; x < listStokPo.size(); x++){
                    StokOpnameModel som = listStokPo.get(x);
                    if(som.getBarang().getIdBarang() == pdm.getBarang().getIdBarang() && som.getBulanKadaluarsa().equals(pdm.getBulanKadaluarsa())
                            && som.getGudang().getIdMasterGudang() == pdm.getGudang().getIdMasterGudang()){
                        som.setQty(som.getQty() + pdm.getQty().intValue());
                        listStokPo.set(x, som);
                    }else{
                        listStokPo.add(som);
                    }
                }
            }
//            List<PengirimanModel> pengirimanModels = MainService.get
        }
        
        
//        JOptionPane.showMessageDialog(null, listStokPo.get(0).getQty());

        for(int y = 0; y < listStok.size(); y++){
            for(StokOpnameModel som : listStokPo){
                StokOpnameModel somFisik = listStok.get(y);
                if (somFisik.getBarang().getIdBarang() == som.getBarang().getIdBarang() && somFisik.getBulanKadaluarsa().equals(som.getBulanKadaluarsa())
                        && somFisik.getGudang().getIdMasterGudang() == som.getGudang().getIdMasterGudang()){
                    if(somFisik.getQtyJual() == null){
                        somFisik.setQtyJual(som.getQty());
                    }else{
                        somFisik.setQtyJual(som.getQty() + somFisik.getQtyJual());
                    }
                    listStok.set(y, somFisik);
                }
            }
        }

        
        return listStok;
    }

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        // TODO add your handling code here:
        if (cboGudang.getSelectedItem() != null && !chkGudang.isSelected()){
            GudangModel g = (GudangModel) cboGudang.getSelectedItem();
            listStok = stokOpnameService.getAvailableGudang(g.getIdMasterGudang());
        }else{
            listStok = stokOpnameService.getAvailable();
        }

        listStok = calculateStokView(listStok);
        refreshTable();
    }//GEN-LAST:event_btnRefreshActionPerformed

    private void cboBarangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboBarangActionPerformed
        // TODO add your handling code here:
        try{
            BarangModel b = (BarangModel) cboBarang.getSelectedItem();
            listStok = stokOpnameService.getAvailableBarang(b.getIdBarang());

            listStok = calculateStokView(listStok);
            refreshTable();
        }catch(Exception e){

        }
    }//GEN-LAST:event_cboBarangActionPerformed

    private void refreshTable(){
        tblStok.setAutoCreateColumnsFromModel(false);
        tblStok.setModel(new StokTableModel(listStok));
    }


    private class StokTableModel extends AbstractTableModel{
        private List<StokOpnameModel>  listStok;

        public StokTableModel(List<StokOpnameModel> listStok) {
            this.listStok = listStok;
        }

        @Override
        public int getRowCount() {
            return listStok.size();
        }

        @Override
        public int getColumnCount() {
            return 5;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
                StokOpnameModel s = listStok.get(rowIndex);
                switch(columnIndex){
                    case 0: return s.getGudang().getNamaGudang();
                    case 1: return s.getBarang().getNamaBarang();
                    case 2: return s.getBulanKadaluarsa();
                    case 4: return s.getQty();
                    case 3: return new SimpleDateFormat("dd/MM/yyyy").format(s.getTanggal());
                    case 5: return s.getQtyJual();
                    case 6: return s.getQty() - s.getQtyJual();
                    default: return "";
                }
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {            
            return false;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            if (columnIndex == 4 || columnIndex == 5 || columnIndex == 6){
                return BigDecimal.class;
            }
            return super.getColumnClass(columnIndex);
        }


    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBySalesOrder;
    private javax.swing.JButton btnRefresh;
    private javax.swing.JComboBox cboBarang;
    private javax.swing.JComboBox cboGudang;
    private javax.swing.JCheckBox chkGudang;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JCheckBox jCheckBox3;
    private javax.swing.JCheckBox jCheckBox4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JTabbedPane jTabbedPane3;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTable tblStok;
    // End of variables declaration//GEN-END:variables

}
